package cn.edu.lingnan.service.image.impl;

import cn.edu.lingnan.bean.Book;
import cn.edu.lingnan.bean.Images;
import cn.edu.lingnan.repository.ImageRepository;
import cn.edu.lingnan.service.image.ImageService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class ImageServiceImpl implements ImageService {

    @Resource
    ImageRepository imageRepository;

    public Images save(Images images){
        return imageRepository.save(images);
    }

    @Override
    public Map<String, Object> page(int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageRequest pageRequest = PageRequest.of(pageNum -1, pageSize, Sort.by(Sort.Direction.DESC,"id"));
        Page<Images> page = imageRepository.findAll(pageRequest);
        map.put("TotalElements",page.getTotalElements());
        map.put("pageContent",page.getContent());
        System.out.println(map + "========map");
        return map;
    }

    //=========编辑============
    //删除对象
    @Override
    public void deleteById(int id) {
        imageRepository.deleteById(id);
    }

    //=========编辑============
    //查询对象
    @Override
    public Images findById(int id) {
        Images img = imageRepository.getOne(id);

        return img;
    }


}
